/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carrello.impl.boundary.user;

import carrello.impl.boundary.AbstractUI;
import carrello.model.entities.Product;
import carrello.impl.controller.user.HomeCTRL;
import carrello.model.entities.Category;
import carrello.model.entitydao.CategoryDAO;
import carrello.util.EntityManagerUtil;
import java.util.List;
import org.apache.log4j.Logger;
/**
 *
 * @author mepcotterell
 */
public class HomeUI extends AbstractUI {
    
    private List<Product> products;
    private List<Category> categories;
    private int numOfCategories = 0;

    private String tree;
    private int numOfProducts = 0;
    Logger log = Logger.getLogger(HomeUI.class);
    
    public String gettree() {
        return tree;
    }
    public void settree (String s ) {
        tree = s;
    }
    public List<Category> getCategories () {
        return categories;
    }
    public List<Product> getProducts () {
        return products;
    }

    
    public boolean getAllProducts (String category) {
        products = HomeCTRL.getAllProducts(category);
        setNumOfProducts(products.size());
        return true;
    }
    
    public boolean getAllCategories () {
        categories = HomeCTRL.getAllCategories();
        setNumOfCategories(categories.size());
        return true;
    }
    public String execute()
    {
        //Category cat = Category.getCategoryByName("All");
        EntityManagerUtil.getEntityManager().getHibernateSession().clear();
        CategoryDAO categoryDAO = new CategoryDAO();
        Category cat = categoryDAO.getByName("All");
        StringBuilder temp = new StringBuilder ();
        temp.append ("<ul id=\"navigation\">");
        getTreeString (cat, temp, 0);
        temp.append ("</ul>");
        tree = temp.toString();
        log.info(getSessionRole());
        getAllCategories();
        getAllProducts ("");
        return INPUT;
    }
    private void getTreeString (Category cat, StringBuilder tree, int i) {

        ++i;
        if ( i == 1 )
            tree.append ("<li  class=\"open\"><a id=\"" + cat.getName() + "\">" + cat.getName() + "</a>");
        else 
            tree.append ("<li><a id=\"" + cat.getName() + "\">" + cat.getName() + "</a>");
        if (cat.getchildren() != null) {
            for (Category c : cat.getchildren()) {
                tree.append("<ul>");
                getTreeString(c, tree, i);
                tree.append("</ul>");
            }
        }
        --i;
        tree.append ("</li>");
        
    }
    /**
     * @return the numOfCategories
     */
    public int getNumOfCategories() {
        return numOfCategories;
    }

    /**
     * @param numOfCategories the numOfCategories to set
     */
    public void setNumOfCategories(int numOfCategories) {
        this.numOfCategories = numOfCategories;
    }

    /**
     * @return the numOfProducts
     */
    public int getNumOfProducts() {
        return numOfProducts;
    }

    /**
     * @param numOfProducts the numOfProducts to set
     */
    public void setNumOfProducts(int numOfProducts) {
        this.numOfProducts = numOfProducts;
    }

}
